from app.product.unit.model import Unit
from exts import db


# ------------- 单位 -------------
# def create_unit(user_id, **kwargs):
#     if not kwargs.get('short_name'):
#         kwargs['short_name'] = get_first_letter_pinyin(kwargs.get("name"))
#     new_obj = DictMixin.from_dict(UnitTable, kwargs)
#     new_obj.creator_id = user_id
#     db.session.add(new_obj)
#     db.session.commit()
#     return new_obj


# def get_by_page(user_id, **kwargs):
#     pagination = dao.get_by_page(**kwargs)
#     pagination = custom_marshal_pagination(pagination)
#     return pagination


# def get(user_id, obj_id):
#     return UnitTable.query.filter_by(id=obj_id).first()
#
#
# def update(user_id, obj_id, **kwargs):
#     obj = UnitTable.query.filter_by(id=obj_id).first()
#     old_obj = deepcopy(obj)
#     new_obj = DictMixin.from_dict(obj, kwargs)
#     new_obj.updater_id = user_id
#     # 当名称发生修改时，自动更新short_name
#     if new_obj.name and old_obj.name != new_obj.name:
#         new_obj.short_name = get_first_letter_pinyin(obj.name)
#     db.session.commit()
#     return new_obj
#
#
# def delete(user_id, obj_id):
#     obj = UnitTable.query.filter_by(id=obj_id).first()
#     db.session.delete(obj)
#     db.session.commit()
#     return obj.id


# def get_default_unit():
#     default_unit = Unit.query.filter_by(is_default=True).first()
#     return default_unit


def create_base_units_script():
    units = ['袋', '件', '把', '套', '吨', '台',
             # '千克', '米', '根', '支', '块', '方',
             # '盆', '板', '渣', '樘', '桶'
             ]
    user_id = 1
    Unit.create(user_id=user_id, name='个', is_default=True)
    for unit_str in units:
        Unit.create(user_id=user_id, name=unit_str)
    db.session.commit()


if __name__ == "__main__":
    from app import create_app

    app = create_app("default")
    with app.app_context():
        create_base_units_script()
